var coupon_cont = require('../../../model/coupon_cont')

Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  properties: {
    objmodel: {
      type: Object,
      default: ''
    },
    datasource: {
      type: Array,
      default: []
    },

  },
  /**
   * 组件的初始数据
   */
  data: {
    modellist: [],
    idx: -1,
    htkeys: {},
  },

  ready() {


    var objmodelmain = this.data.objmodel.main;
    var that = this;
    var couponids = [];
    // objmodelmain.vtype=='customcoupon'

    var condition = {};
    if (objmodelmain.sourceform == 'custom') {

      var datalist = objmodelmain.datalist;

      if (datalist) {
        for (var a in datalist) {
          couponids.push(datalist[a]["id"]);
        }
        condition["couponids"] = couponids.join(",");
      }
    } else {
      if (objmodelmain.condition) {
        condition = objmodelmain.condition
      }
    }
    coupon_cont.procoupon(0, condition).then(function (data) {

      if (data["status"] == 1 && data["obj"]) {

        var len = data["obj"].length - 1;
        while (len >= 0) {
          var temitem = data["obj"][len]
          if (temitem.myreceivednum == temitem.everyonelimit || temitem.receivednum == temitem.num) {
            //没达到 的删除
            data["obj"].splice(len, 1);

          }
          len--
        }
        if (data["obj"].length < 1) {
          return;
        }

        var htkeys = {
          name2: "折扣券",
          name1: "优惠券",
          v1: objmodelmain.decrement,
          v2: objmodelmain.discount
        }
        var modellist = data["obj"]

        if (objmodelmain.sourceform == 'custom') {

          modellist = that.transformdata(datalist, modellist)
        }




        that.setData({
          modellist,
          htkeys
        })
      }
    })


    return
    // that.setData({modellist:objmodelmain.datalist})
    //   if(objmodelmain.sourceform=='custom'){
    //     var   datalist=objmodelmain.datalist;
    //     if(datalist)
    //     {
    //        for(var a in datalist)
    //        {
    //         couponids.push(datalist[a]["id"]);
    //        }
    //     }
    //     that.setData({modellist:objmodelmain.datalist})
    //   }
    //   else
    //   {
    //     coupon_cont.procoupon(0).then(function(data){    

    //       if(data["status"]==1&&data["obj"]){

    //         var len=data["obj"].length-1;  
    //            while(len>=0){
    //              var temitem=data["obj"][len]
    //              if(temitem.myreceivednum==temitem.everyonelimit||temitem.receivednum==temitem.num) {
    //               //没达到 的删除
    //               data["obj"].splice(len,1);

    //              }
    //              len--
    //            }
    //            if(data["obj"].length<1)
    //            {
    //              return ;
    //            }      
    //        var htkeys={name2:"折扣券",name1:"优惠券",v1:objmodelmain.decrement,v2:objmodelmain.discount}   
    //        that.setData({modellist:data["obj"],htkeys})
    //       }      
    //    })

    //   }


  },
  /**
   * 组件的方法列表
   */
  methods: {
    transformdata(datalist, modellist) {
      var newmodellist = []

      if (datalist && datalist.length > 0 && modellist && modellist.length > 0) {
        datalist.forEach(item => {
          var newitem = modellist.find(tem => {
            return tem.id == item.id
          })
          if (newitem) {
            newitem["simg"] = item.simg;
            newmodellist.push(newitem)
          }
        })
      }
      return newmodellist

    },

    getcouponinfo(e) {
      var app = getApp();
    

      var that=this;
      app.xc_sys_config.authop.oppower("login").then(function (res) {

        var dataset = e.currentTarget.dataset
        var item = dataset.item
        that.data.idx = dataset.idx
        var getcouponinfo = that.selectComponent("#getcouponinfo");
        getcouponinfo.showmodel(item);
      })

    },
    receivecoupon(e) {
      var app = getApp();

      var that=this;
      var item=e.currentTarget.dataset.item;
      var index=e.currentTarget.dataset.index;

      console.log(item)


      app.xc_sys_config.authop.oppower("login").then(function (res) {
        coupon_cont.receivecoupon(item.id).then(res => {
           that. _receivecouponchange(index,res)
        })
      })


   
   

     
       

    },
    receivecouponchange(ev) {

      var idx = this.data.idx
      var res = ev.detail
      this._receivecouponchange(idx,res)
    },

    _receivecouponchange(idx,res){
      var that = this;

      var updatemodel = {}
      var keyval = "modellist[" + idx + "]."
      if (res["status"] == 1) {
        updatemodel[keyval + "myreceivednum"] = res["obj"].myreceivednum
        updatemodel[keyval + "receivednum"] = res["obj"].receivednum
        that.setData(updatemodel)
        wx.showToast({
          title: '领取成功',
        })
      } else {


        wx.showModal({
          title: '错误',
          content: res["message"],
          showCancel: false

        })

        if (res["status"] == -1) {

        } else if (res["status"] == 2) {
          updatemodel[keyval + "receivednum"] = this.data.modellist[idx].num;
          that.setData(updatemodel)
        } else if (res["status"] == 3) {
          updatemodel[keyval + "myreceivednum"] = this.data.modellist[idx].everyonelimit
          that.setData(updatemodel)
        }
      }
    },
  }
})